package com.example.auv_660.command;

import com.example.auv_660.moduleload.BatteryLoad;
import com.example.auv_660.moduleload.ModuleLoad;
import com.example.auv_660.network.tcp.HFTCPBuild;
import com.example.auv_660.network.tcp.ModuleReadHandler;
import com.example.auv_660.network.tcp.ModuleWriteHandler;

import java.util.ArrayList;

public class AUVBatteryCommand extends AUVCommand {
    private ArrayList<ModuleLoad> moduleLoadArrayList;
    private static final String BATTERY = "BatteryLoad";
    private int number = -1;

    public AUVBatteryCommand()
    {
        moduleLoadArrayList = HFTCPBuild.INSTANCE.getModuleInstance();
        number = getAUVModuleNumber(BATTERY);
    }

    @Override
    public void writeCommand(String command) {
        if (number != -1 && moduleLoadArrayList.size() - number >= 0)
        {
            ModuleWriteHandler batteryWriteHandler = moduleLoadArrayList.get(number).getWriterHandler();
            if (batteryWriteHandler != null)
            {
                batteryWriteHandler.sendCommandToModule(command);
            }
        }
    }

    @Override
    public void readCommand() {
        if (number != -1 && moduleLoadArrayList.size() - number >= 0)
        {
            ModuleReadHandler batteryReadHandler = moduleLoadArrayList.get(number).getReadHandler();
            if (batteryReadHandler != null)
            {
                batteryReadHandler.getMessageFromModule();
            }
        }
    }

    public String getBatteryValue()
    {
        if (number != -1)
        {
            return ((BatteryLoad) moduleLoadArrayList.get(number)).getBatteryValue();
        }
        return "0%";
    }
}
